package com.leetcode.questiononehundardandfifty.双指针;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author wtcode
 * @Description
 * @date 2025/3/25 - 18:16
 */
public class Answer {

    public static void main(String[] args) {

        isSubsequence("abc","weweabcfg");
    }


    // 验证回文串
    public static boolean isPalindrome(String s) {
        s = s.trim();
        int len = s.length();
        int left = 0;
        int right = len - 1;
        while (left < right){
            if(!Character.isLetterOrDigit(s.charAt(left))){
                left++;
            }else if(!Character.isLetterOrDigit(s.charAt(right))){
                right--;
            }else if(Character.toLowerCase(s.charAt(left)) == Character.toLowerCase(s.charAt(right))){
                left++;
                right--;
            }else {
                return false;
            }
        }
        return true;

    }

    //判断s是否为t的子序列
    public static boolean isSubsequence(String s, String t) {
        int i = 0;
        int j = 0;
        while(i < s.length() && j < t.length()){
            if(s.charAt(i) == t.charAt(j)){
                i++;
            }
            j++;
        }
        return i == s.length();

    }

    // 两数之和，输入有序数组
    public static int[] twoSum(int[] numbers, int target) {
        int left = 0;
        int right = numbers.length - 1;
        while(left < right){
            int sum = numbers[left] + numbers[right];
            if(sum == target){
                return new int[]{left+1,right+1};
            }else if(sum < target){
                left++;
            }else {
                right--;
            }

        }
        return new int[]{-1,-1};

    }

    // 容纳最多的水
    public static int maxArea(int[] height) {
        int left = 0;
        int right = height.length - 1;
        int res = 0;

        while (left < right){
            if(height[left] < height[right]){
                res = Math.max(res,height[left] * (right-left));
                left++;
            }else {
                res = Math.max(res,height[right] * (right-left));
                right--;
            }
        }

        return res;

    }


    public static List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> res = new ArrayList();
        Arrays.sort(nums);
        int len = nums.length;
        for (int k = 0; k < len - 2; k++) {
            if(nums[k] > 0) break;
            if(k > 0 && nums[k] == nums[k-1]) continue;
            int i = k+1;
            int j = len - 1;
            while (i < j){
                int sum = nums[k] + nums[i] + nums[j];
                if(sum < 0){
                    while (i < j && nums[i] == nums[++i]);
                }else if(sum >0){
                    while (i < j && nums[j] == nums[--j]);
                }else {
                    List<Integer> list = new ArrayList();
                    list.add(nums[k]);
                    list.add(nums[i]);
                    list.add(nums[j]);
                    res.add(list);
                    while(i < j && nums[i] == nums[++i]);
                    while(i < j && nums[j] == nums[--j]);
                }

            }

        }
        return res;

    }



}
